package expenager.project;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Transactions extends Activity {
	Calendar calendar;
	TextView textSign, textDate;
	Spinner spinPaymentMethod, spinIncomes, spinExpenses;
	RadioButton radioIncome, radioExpense;
	EditText editTextComment, editTextAmount;
	Button btnActivities, btnStatistics, btnCalculator, 
	transactionOk, transactionCancel;
	RadioGroup transactionType;
	String dates, months, years;
	
	@Override
	public void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transactions);
		
		//change to Activities Page
		btnActivities = (Button) findViewById(R.id.buttonTransactionActivities);
		btnActivities.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//start Activities page activity
				Intent i = new Intent(getApplicationContext(),Activities.class);
				startActivity(i);
			}
		});
		
		//change to Statistics Page
		btnActivities = (Button) findViewById(R.id.buttonTransactionStatistics);
		btnActivities.setOnClickListener(new View.OnClickListener() {
					
			public void onClick(View v) {
				//start Activities page activity
				Intent i = new Intent(getApplicationContext(),Statistics.class);
				startActivity(i);
			}
		});
		
		//use calculator function
		btnCalculator = (Button) findViewById(R.id.buttonCalc);
		btnCalculator.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(),Calculator.class);
				startActivity(i);
			}
		});	
		
		//get current date from Android calendar
		setDate();
		
		//get sign from DB setting
		textSign = (TextView) findViewById(R.id.textViewSign);
		textSign.setText(getCurrency());
		
		editTextComment = (EditText) findViewById(R.id.editTextComment);
		
		//get payment method from DB setting
		spinPaymentMethod = (Spinner) findViewById(R.id.spinPaymentMethod);	
		spinPaymentMethod.setAdapter(getItemPaymentMethod());
		
		transactionType = (RadioGroup) findViewById(R.id.radioTransactionType);
		
		//choose what transaction type will be displayed
		radioIncome = (RadioButton) findViewById(R.id.radioIncome);
		radioIncome.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				spinExpenses.setVisibility(View.GONE);
				spinIncomes.setVisibility(View.VISIBLE);
			}
		});
		
		radioExpense = (RadioButton) findViewById(R.id.radioExpense);
		radioExpense.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				spinIncomes.setVisibility(View.GONE);
				spinExpenses.setVisibility(View.VISIBLE);
			}
		});
		//...end of choose
		
		//get income type from DB setting
		spinIncomes = (Spinner) findViewById(R.id.spinIncomes);	
		spinIncomes.setAdapter(getItemIncome());
		
		//get expense type from DB setting
		spinExpenses = (Spinner) findViewById(R.id.spinExpenses);	
		spinExpenses.setAdapter(getItemExpense());
		spinExpenses.setVisibility(View.GONE);
		
		editTextAmount = (EditText) findViewById(R.id.editTextAmount);
		
		transactionOk = (Button) findViewById(R.id.btnTransactionOk);
		transactionOk.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//check amount text box is not empty
				if(!"".equals(editTextAmount.getText().toString())) {
				
				String type = "";
				String category = "";
				String payment = "";
				String comment = "";
				int test;
				double amount;
				
				test = transactionType.getCheckedRadioButtonId();
				
				//check which transaction type is selected
				if(test==radioIncome.getId()) {
					type="Income";
					category = spinIncomes.getSelectedItem().toString();
				}
				else if(test==radioExpense.getId()) {
					type="Expense";
					category = spinExpenses.getSelectedItem().toString();
				}
				else type="ERROR";
				
				payment = spinPaymentMethod.getSelectedItem().toString();
				comment = editTextComment.getText().toString();
				amount = Double.parseDouble(editTextAmount.getText().toString());
				
				//add to DBHistory
				addTransaction(dates,months,years,type,category,payment,amount,comment);
				}
				
				//generate when the amount text box is empty
				else Toast.makeText(getApplicationContext(), "Please enter the transaction's Amount", Toast.LENGTH_SHORT).show();
			}
		});
		
		transactionCancel = (Button) findViewById(R.id.btnTransactionCancel);
		transactionCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});
		
	}
	
	//get Currency sign from db
	public String getCurrency() {
		DBAdapter db = new DBAdapter(this);
		db.open();
		Cursor c = db.getSettingSign();
		String sign = c.getString(0).toString();
		db.close();
		return sign;
	}
	
	//get current date from calendar
	public void setDate() {
		calendar = Calendar.getInstance();
		
		SimpleDateFormat day_num = new SimpleDateFormat("EEE");
		String day = day_num.format(calendar.getTime());
		
		dates = Integer.toString(calendar.get(Calendar.DATE));
		
		SimpleDateFormat month_num = new SimpleDateFormat("MMM");
		months = month_num.format(calendar.getTime());
		
		years = Integer.toString(calendar.get(Calendar.YEAR));
		
		textDate = (TextView) findViewById(R.id.textViewDateTransaction);
		textDate.setText(day+", "+dates+" "+months+" "+years);
	}
	
	//get payment method from db
	public ArrayAdapter<String> getItemPaymentMethod() {
		DBAdapter db = new DBAdapter(this);
		List<String> listPaymentMethod = new ArrayList<String>();
		
		db.open();
		Cursor c = db.getSettingPaymentMethod();
		for(int i=0;i<c.getCount();i++) {
			listPaymentMethod.add(c.getString(0).toString());
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		db.close();
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listPaymentMethod);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return adapter;
	}
	
	//get income type from db
	public ArrayAdapter<String> getItemIncome() {
		DBAdapter db = new DBAdapter(this);
		List<String> listIncome = new ArrayList<String>();

		db.open();
		Cursor c = db.getSettingIncome();
		for(int i=0;i<c.getCount();i++) {
			listIncome.add(c.getString(0).toString());
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		db.close();
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listIncome);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return adapter;
	}
	
	//get expense type from db
	public ArrayAdapter<String> getItemExpense() {
		DBAdapter db = new DBAdapter(this);
		List<String> listExpense = new ArrayList<String>();
		
		db.open();
		Cursor c = db.getSettingExpense();
		for(int i=0;i<c.getCount();i++) {
			listExpense.add(c.getString(0).toString());
			
			if(i!=(c.getCount()-1)) {
				c.moveToNext();
				}
		}
		db.close();
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listExpense);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return adapter;
	}
	
	//add transaction to history db
	public void addTransaction(String date, String month, String year, String type, String category, String payment, double amount, String comment) {
		DBHistory db = new DBHistory(this);
		
		db.open();
		db.addHistory(date,month,year,type,category,payment,amount,comment);
		db.close();
		
		Toast.makeText(getApplicationContext(), "New transaction saved", Toast.LENGTH_SHORT).show();
		
		//end of Transaction Page. Back to Main Page	
		finish();
	}
}
